import {
    getBreadCrumbList,
    getDynamicOpenTab,
    getHomeRoute,
    getNextRoute,
    getRouteTitleHandled,
    getTagNavListFromSessionStorage,
    localRead,
    localSave,
    routeEqual,
    routeHasExist,
    setDynamicOpenTab,
    setTagNavListInSessionStorage
} from '@/libs/menu/util'
import {otherRouter} from '@/router/router';
import {router} from '@/router/index';
import config from '@/config'

const homeName = config.getHomeName()

const closePage = (state, route,turnFlag) => {
    const nextRoute = getNextRoute(state.tagNavList, route)
    state.tagNavList = state.tagNavList.filter(item => {
        return !routeEqual(item, route)
    })
    if(turnFlag) {
        router.push(nextRoute)
    }
}
const processTag =(state,route)=>{
    let tag = state.tagNavList.filter(item => routeEqual(item, route))
    route = tag[0] ? tag[0] : null
    if (!route) return
    if (route.meta.dynamicTab) {
        setDynamicOpenTab(getDynamicOpenTab().filter(tab => {
            return tab.meta.dIndex != route.meta.dIndex
        }))
    }
}

export default {
    state: {
        breadCrumbList: [],
        tagNavList: [...otherRouter.children],
        homeRoute: {},
        local: localRead('local'),
        routers: [
            otherRouter
        ],
        menuList: [],
        hasReadErrorPage: false,
        dynamicTab: {
            tabIndex: '',
            uuid: 0
        }
    },
    mutations: {
        // 动态添加主界面路由，需要缓存
        updateAppRouter(state, routes) {
            state.routers.push(...routes);
            router.addRoutes(routes);
        },
        // 动态添加全局路由，不需要缓存
        updateDefaultRouter(state, routes) {
            router.addRoutes(routes);
        },
        updateMenuList(state, routes) {
            state.menuList = routes;
        },
        setBreadCrumb(state, {route, breadCrumbList}) {
            if (breadCrumbList) {
                state.breadCrumbList = breadCrumbList;
            } else {
                state.breadCrumbList = getBreadCrumbList(route, state.homeRoute)
            }
        },
        setHomeRoute(state) {
            state.homeRoute = getHomeRoute(state.routers, homeName)
        },
        clearTagNavList(state) {
            state.tagNavList = [];
            setTagNavListInSessionStorage([])
            setDynamicOpenTab([])
        },
        setTagNavList(state, list) {
            let tagList = []
            if (list) {
                tagList = [...list]
            } else tagList = getTagNavListFromSessionStorage() || []
            if (tagList[0] && tagList[0].name !== homeName)
                tagList.shift()
            let homeTagIndex = tagList.findIndex(item => item.name === homeName)
            if (homeTagIndex > 0) {
                let homeTag = tagList.splice(homeTagIndex, 1)[0]
                tagList.unshift(homeTag)
            }
            state.tagNavList = tagList
            setTagNavListInSessionStorage([...tagList])
        },
        closeTag(state, route) {
            processTag(state,route)
            closePage(state, route,true)
        },
        closeOtherTag(state, route) {
            processTag(state,route)
            closePage(state, route,false)
        },
        addTag(state, {route, type = 'unshift'}) {
            let router = getRouteTitleHandled(route)
            if (!routeHasExist(state.tagNavList, router)) {
                if (type === 'push')
                    state.tagNavList.push(router)
                else {
                    if (router.name === homeName) state.tagNavList.unshift(router)
                    else state.tagNavList.splice(1, 0, router)
                }
                setTagNavListInSessionStorage([...state.tagNavList])
            }
        },
        setLocal(state, lang) {
            localSave('local', lang)
            state.local = lang
        },
        updateDynamicTab(state, {tabIndex, uuid}) {
            state.dynamicTab.tabIndex = tabIndex;
            state.dynamicTab.uuid = uuid;
        }
    },
    actions: {
        addNewTab({state, commit}, route) {
            const dynamicTabs = getDynamicOpenTab();

            let localSaved = dynamicTabs.filter(dTab => {
                    return dTab.meta.dIndex === route.children[0].meta.dIndex
                }).length > 0,
                routerAdd = state.routers.filter(dTab => {
                    return dTab.name === route.name;
                }).length > 0
            ;
            if (!localSaved) {
                dynamicTabs.push(route.children[0]);
                setDynamicOpenTab(dynamicTabs);
            }
            if (!routerAdd) {
                commit('updateAppRouter', [route]);
            }
        }
    }
}
